﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Kele.Activity.Core;
using Kele.Core.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Kele.Core.Http;

namespace Kele.Activity.Web.Controllers.V1
{
    [Route("api/file")]
    [Authorize]
    [ApiController]
    public class FileController : SiteApiController
    {
        [HttpPost]
        [Route("upload")]
        public async Task<IActionResult> UploadFile(IFormFile file)
        {
            if (file == null || file.Length == 0) return OkResult(new { code = 2000 });

            var data = await FileService.CloudFileUpload(MerchantId, file);
            if (data == null) return OkResult(new { code = 2000 });
            return OkResult(new
            {
                code = 1000,
                data
            });
        }

        [HttpPost]
        [Route("delete")]
        public async Task<IActionResult> DeleteFile()
        {
            var uid = Request.Params("uid");
            if (string.IsNullOrWhiteSpace(uid)) return ErrorResult("uid 是需要填的");
            var merchant_id = Convert.ToInt32(User.Claims.First(claim => claim.Type == "merchant_id").Value);
            await AppService.File.QiniuDelete(merchant_id, uid);
            return OkResult(new { code = 1000 });
        }
    }
}